/**
 * Copyright &copy; 2012-2016   All rights reserved.
 */
package com.kingeid.website.modules.slideshow.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kingeid.framework.common.config.Global;
import com.kingeid.framework.common.persistence.Page;
import com.kingeid.framework.common.web.BaseController;
import com.kingeid.framework.common.utils.StringUtils;
import com.kingeid.website.modules.slideshow.entity.WebsiteSlideshow;
import com.kingeid.website.modules.slideshow.service.WebsiteSlideshowService;

/**
 * 轮播图管理Controller
 * @author lanjh
 * @version 2019-05-17
 */
@Controller
@RequestMapping(value = "${adminPath}/slideshow/websiteSlideshow")
public class WebsiteSlideshowController extends BaseController {

	@Autowired
	private WebsiteSlideshowService websiteSlideshowService;
	
	@ModelAttribute
	public WebsiteSlideshow get(@RequestParam(required=false) String id) {
		WebsiteSlideshow entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = websiteSlideshowService.get(id);
		}
		if (entity == null){
			entity = new WebsiteSlideshow();
		}
		return entity;
	}
	
	@RequiresPermissions("slideshow:websiteSlideshow:view")
	@RequestMapping(value = {"list", ""})
	public String list(WebsiteSlideshow websiteSlideshow, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<WebsiteSlideshow> page = websiteSlideshowService.findPage(new Page<WebsiteSlideshow>(request, response), websiteSlideshow); 
		model.addAttribute("page", page);
		return "modules/slideshow/websiteSlideshowList";
	}

	@RequiresPermissions("slideshow:websiteSlideshow:view")
	@RequestMapping(value = "form")
	public String form(WebsiteSlideshow websiteSlideshow, Model model) {
		model.addAttribute("websiteSlideshow", websiteSlideshow);
		return "modules/slideshow/websiteSlideshowForm";
	}

	@RequiresPermissions("slideshow:websiteSlideshow:edit")
	@RequestMapping(value = "save")
	public String save(WebsiteSlideshow websiteSlideshow, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, websiteSlideshow)){
			return form(websiteSlideshow, model);
		}
		websiteSlideshowService.save(websiteSlideshow);
		addMessage(redirectAttributes, "保存轮播图成功。");
		return "redirect:"+Global.getAdminPath()+"/slideshow/websiteSlideshow/?repage";
	}
	
	@RequiresPermissions("slideshow:websiteSlideshow:edit")
	@RequestMapping(value = "delete")
	public String delete(WebsiteSlideshow websiteSlideshow, RedirectAttributes redirectAttributes) {
		websiteSlideshowService.delete(websiteSlideshow);
		addMessage(redirectAttributes, "删除轮播图成功");
		return "redirect:"+Global.getAdminPath()+"/slideshow/websiteSlideshow/?repage";
	}

}